﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Example2.Model
{
    class Ball
    {


        public Vector2 m_centerPosition = new Vector2(5 , 5);
        public Vector2 m_oldCenterPosition = new Vector2(5, 5);
        public float m_radius = 0.25f;


        public Vector2 m_velocity = new Vector2(-1, 5);
        private Vector2 m_gravity = new Vector2(0, 0.01f);

        internal void Update(float a_elapsedTimeSeconds)
        {
            //Räknar ut nya värden på hastighet och position med hjälp utav integration

            InterpolateVelocity(a_elapsedTimeSeconds);

            m_oldCenterPosition = m_centerPosition;
            m_centerPosition = InterpolatePosition(a_elapsedTimeSeconds);
        }

        private Vector2 InterpolatePosition(float a_elapsedTimeSeconds)
        {
            //s1 = s0 + var * t
            Vector2 newCenterPosition = m_centerPosition + m_velocity * a_elapsedTimeSeconds;

            return newCenterPosition;
        }

        private void InterpolateVelocity(float a_elapsedTimeSeconds)
        {
            //v1 = v0 + a *t
            m_velocity = m_velocity +m_gravity * a_elapsedTimeSeconds;
        }

        internal void CollideWalls(int levelWidth)
        {
            if (m_centerPosition.X + m_radius > levelWidth)
            {
                m_velocity.X = -m_velocity.X;
                m_centerPosition.X = levelWidth  - m_radius;
            }
            if (m_centerPosition.X - m_radius < 0)
            {
                m_velocity.X = -m_velocity.X;
                m_centerPosition.X = m_radius;
            }
            if (m_centerPosition.Y - m_radius < 0)
            {
                m_velocity.Y = -m_velocity.Y;
                m_centerPosition.Y = m_radius;
            }
        }
    }
}
